package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysExamPaper;
import com.ruoyi.system.service.ISysExamPaperService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 试卷信息Controller
 *
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/paper")
public class SysExamPaperController extends BaseController {
    private final ISysExamPaperService sysExamPaperService;

    @PreAuthorize("@ss.hasPermi('system:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysExamPaper sysExamPaper) {
        startPage();
        List<SysExamPaper> list = sysExamPaperService.selectSysExamPaperList(sysExamPaper);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('system:paper:export')")
    @Log(title = "试卷信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysExamPaper sysExamPaper) {
        List<SysExamPaper> list = sysExamPaperService.selectSysExamPaperList(sysExamPaper);
        ExcelUtil<SysExamPaper> util = new ExcelUtil<SysExamPaper>(SysExamPaper.class);
        util.exportExcel(response, list, "试卷信息数据");
    }

    @PreAuthorize("@ss.hasPermi('system:paper:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysExamPaperService.selectInfoById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:paper:add')")
    @Log(title = "试卷信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysExamPaper sysExamPaper) {
        return toAjax(sysExamPaperService.insert(sysExamPaper));
    }

    @PreAuthorize("@ss.hasPermi('system:paper:edit')")
    @Log(title = "试卷信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysExamPaper sysExamPaper) {
        return toAjax(sysExamPaperService.update(sysExamPaper));
    }

    @PreAuthorize("@ss.hasPermi('system:paper:remove')")
    @Log(title = "试卷信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysExamPaperService.removeBatchByIds(Arrays.asList(ids)));
    }


    @Anonymous
    @GetMapping("/getExamPapers")
    public AjaxResult getExamPapers() {
        return success(sysExamPaperService.selectSysExamPaperList(new SysExamPaper()));
    }

    @Anonymous
    @GetMapping("/getExamPaper/{id}")
    public AjaxResult getExamPaperInfo(@PathVariable Long id) {
        return success(sysExamPaperService.selectInfoById(id));
    }
}
